// pages/borrow_book/borrow_book.js
const app = getApp();
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentSwiper: 0,
    array: [
      "书籍",
      "服饰",
      "鞋子",
      "电器",
      "手机",
      "零食",
      "数码"
    ],
    map_array:[
      {items: null,number:0},
      { items: null, number: 0 },
      { items: null, number: 0 },
      { items: null, number: 0 },
      { items: null, number: 0 },
      { items: null, number: 0 },

      { items: null, number: 0 }
    ],
    number:7,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let name ="map_array[0].items"
    let that=this;
    db.collection('users').where({
      species:that.data.array[0]
    })
      .get({
        success:function(res){
          that.setData({
            [name]:res.data,
            number:res.data.length
          })
        },
        fail: console.error
      })
  },
  //去指定的index
  goindex:function(e){
    let num=e.currentTarget.dataset.num;
    this.setData({
      currentSwiper: num
    })
  },
  //改变转轮
  swiperChange: function (e) {
    let that=this;
    let index = e.detail.current;
    this.setData({
      currentSwiper: e.detail.current
    })
    //暂时不搞下拉刷新
    if (that.data.map_array[e.detail.current].items==null){
      let name ="map_array["+index+"].items"
      db.collection('users').where({
        species: that.data.array[index]
      })
        .get({
          success: function (res) {
            that.setData({
              [name]: res.data,
              number: res.data.length
            })
            console.log(that.data.map_array[index].items);
          },
          fail: console.error
        })
    }else{
      //设置当前商品数目
      that.setData({
        number: that.data.map_array[index].items.length
      })
    }
  },
  scrollToTop() {
    this.setAction({
      scrollTop: 0
    })
  },
  onReachBottom: function () {
    console.log("到达底部")
  },
  //去一个商品的详细页面
  tomore:function(e){
    let id = e.currentTarget.dataset.id ;
    wx.navigateTo({
      url: '../shop_info/shop_info?id='+id
    })
  }
})